package com.example.redisdelayqueue.controller;

import com.example.redisdelayqueue.common.RedissonDelayQueue;
import com.example.redisdelayqueue.dto.DelayQueueDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/delayQueue/")
@Slf4j
public class DelayQueueController {

    @Autowired
    private RedissonDelayQueue redissonDelayQueue;

    @PostMapping("pushQueue")
    public void pushQueue(@RequestBody DelayQueueDto<String> delayQueueDto) {
        redissonDelayQueue.pushDelayQueue(delayQueueDto.getValue(), delayQueueDto.getDelay(), TimeUnit.SECONDS, delayQueueDto.getQueueCode());
    }

    @GetMapping("popQueue")
    public void popQueue(String queueCode) {
        try {
            String queue = redissonDelayQueue.popDelayQueue(queueCode);
            if (queue.isEmpty()) {
                log.info("暂无可用队列");
                return;
            }
            log.info("出队成功，queueCode {{}}, queue {{}}", queueCode, queue);
        } catch (InterruptedException e) {
            log.error("获取队列消息失败， queueCode {}", queueCode);
            Thread.currentThread().interrupt();
        }
    }
}
